<?php
if (count($_GET)) {
	$result['get'] = $_GET;
}
if (count($_POST)) {
	$result['post'] = $_POST;
}
if (count($_FILES)) {
	$result['files'] = $_FILES;
}
 
// we kill an old file to keep the size small
if (file_exists('script.log') && filesize('script.log') > 102400) {
	unlink('script.log');
}
 
$log = @fopen('script.log', 'a');
if ($log) {
	fputs($log, print_r($result, true) . "\n---\n");
	fclose($log);
}
 
 
// Validation
 
$error = false;
 
//if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
//	$error = 'Invalid Upload';
//}
//
//if (!$error && ($size[0] < 25) || ($size[1] < 25)){
//	$error = 'Please upload an image bigger than 25px.';
//}
//
//if (!$error && $_FILES['Filedata']['size'] > 2 * 1024 * 1024)
//{
//	$error = 'Please upload only files smaller than 2Mb!';
//}
// 
//if (!$error && !($size = @getimagesize($_FILES['Filedata']['tmp_name']) ) )
//{
//	$error = 'Please upload only images, no other files are supported.';
//}
// 
//if (!$error && !in_array($size[2], array(1, 2, 3, 7, 8) ) )
//{
//	$error = 'Please upload only images of type JPEG, GIF or PNG.';
//}

 
 
// Processing
if ($error) {
	$return = array(
		'error' => true,
		'msg' => $error
	);
 
} else {
	//get the file mime
	$return = array(
		'error' => false,
		//'name' => $_FILES['Filedata']['name'],
		//'src' => '/uploads/' . $_FILES['Filedata']['name'],
		
		// Our processing, we get a hash value from the file
		'name' => md5_file($_FILES['Filedata']['tmp_name'])."[".$_FILES['Filedata']['name']."]"
	);
 
	// ... and if available, we get image data
	$info = @getimagesize($_FILES['Filedata']['tmp_name']);
	if ($info) {
		$return['width'] = $info[0];
		$return['height'] = $info[1];
		$return['mime'] = $info['mime'];
	}
	
	//move the file
	
 	move_uploaded_file($_FILES['Filedata']['tmp_name'],  'system/cache/'.$return['name']);
}
 
 
// Output
if (isset($_REQUEST['response']) && $_REQUEST['response'] == 'xml') {
	// header('Content-type: text/xml');
 
	// Really dirty, use DOM and CDATA section!
	echo '<response>';
	foreach ($return as $key => $value) {
		echo "<$key><![CDATA[$value]]></$key>";
	}
	echo '</response>';
} else {
	// header('Content-type: application/json');
	echo json_encode($return);
}